import * as ExcelJS from 'exceljs';
import { saveAs } from 'file-saver';
import _ from 'lodash';

export const table1 = (param) => {
  let data = [];
  for (var key in param) {
    data.push({ name: key, states: param[key] });
  }
  const columns = [
    {
      code: 'id',
      name: '场站',
      width: 200,
      render: (text) => (
        <span>
          <span>{text}</span>
          <span style={{ color: 'rgba(255, 255, 255, 0.6)' }}>(MWh)</span>
        </span>
      ),
    },
    ...data.map(({ name, states }) => ({
      code: name,
      name: name,
      align: 'center',
      // getCellProps: () => ({
      //   style: { background: "#FFF258", color: '#fff' }
      // })
    })),
  ];
  const mockData = (state) => {
    let temp = {};
    data.map(({ name, states }) => {
      temp[name] = states[state]?.toFixed(2) || 0;
    });
    return temp;
  };
  let dataSource = [];
  if (data.length > 0) {
    dataSource = [
      { id: '月度中长期电量', ...mockData('monthly_long_term_quantity') },
      { id: '已分解月度中长期电量', ...mockData('decomposed_monthly_long_term_quantity') },
      { id: '月剩余中长期电量', ...mockData('monthly_residue_long_term_quantity') },
      { id: '日申报中长期电量', ...mockData('day_report_long_term_quantity') },
      { id: '日出清中长期电量', ...mockData('day_clear_long_term_quantity') },
    ];
  }
  return {
    columns,
    dataSource,
    primaryKey: 'id',
  };
};

export const table2 = (param) => {
  let data = [];
  for (var key in param) {
    data.push({ name: key, states: param[key] });
  }
  const columns = [
    {
      code: 'id',
      name: '场站',
      width: 200,
      render: (arr) => (
        <span>
          <span>{arr[0]}</span>
          <span style={{ color: 'rgba(255, 255, 255, 0.6)' }}>{arr[1]}</span>
        </span>
      ),
    },
    ...data.map(({ name, states }) => ({
      code: name,
      name: name,
      align: 'center',
      // getCellProps: () => ({
      //   style: { background: "#8DB4E2", color: '#fff' }
      // })
    })),
  ];
  const mockData = (state) => {
    let temp = {};
    data.map(({ name, states }) => {
      temp[name] = states[state]?.toFixed(2) || 0;
    });
    return temp;
  };
  let dataSource = [];
  if (data.length > 0) {
    dataSource = [
      { id: ['电量', '(MWh)'], ...mockData('longTermQuantity') },
      { id: ['估算均价', '(元/MWh)'], ...mockData('longTermEstimatedAveragePrice') },
      { id: ['电费', '万元'], ...mockData('longTermIncome') },
      { id: ['日前出清电量', '(MWh)'], ...mockData('dayaheadClearingQuantity') },
      { id: ['日前正现货电量', '(MWh)'], ...mockData('dayaheadClearingPlusSpotQuantity') },
      { id: ['日前负现货电量', '(MWh)'], ...mockData('dayaheadClearingMinusSpotQuantity') },
      { id: ['日前正现货电价', '(元/MWh)'], ...mockData('dayaheadClearingPlusSpotPrice') },
      { id: ['日前偏差电费', '万元'], ...mockData('dayaheadDeviationIncome') },
      { id: ['实时上网电量', '(MWh)'], ...mockData('realtimeOngridQuantity') },
      { id: ['实时正现货电量', '(MWh)'], ...mockData('realtimeClearingPlusSpotQuantity') },
      { id: ['实时负现货电量', '(MWh)'], ...mockData('realtimeClearingMinusSpotQuantity') },
      { id: ['实时正现货电价', '(元/MWh)'], ...mockData('realtimeClearingPlusSpotPrice') },
      { id: ['实时负现货电价', '(元/MWh)'], ...mockData('realtimeClearingMinusSpotPrice') },
      { id: ['实时偏差电费', '万元'], ...mockData('realtimeDeviationIncome') },
      { id: ['省间现货电量', '(MWh)'], ...mockData('provincesSpotQuantity') },
      { id: ['省间现货电价', '(元/MWh)'], ...mockData('provincesSpotPrice') },
      { id: ['合计电费', '万元'], ...mockData('totalIncome') },
      { id: ['当日平均电价', '(元/MWh)'], ...mockData('currentAvgPrice') },
      { id: ['本月累计平均电价', '(元/MWh)'], ...mockData('monthAccumulateAvgPrice') },
    ];
  }
  return {
    columns,
    dataSource,
    primaryKey: 'id',
  };
};

// export const table3 = (data) => {
//   const columns = [
//     {
//       code: 'id',
//       name: '',
//       getCellProps: (v, row) => !!row.colSpan && { colSpan: row.colSpan },
//     },
//     ...data.map(({ value, label }) => ({
//       code: value,
//       name: label,
//       align: 'center',
//       // getCellProps: (v, row) => (!row.colSpan && {
//       //   style: { background: "#DDD9C4", color: '#fff' }
//       // })
//     })),
//   ];
//   const mockData = () => {
//     let temp = {};
//     data.map(({ value }) => {
//       temp[value] = _.random(0, 100);
//     });
//     return temp;
//   };
//   const dataSource = [
//     { id: '中长期电量', colSpan: data.length + 1 },
//     { id: '中长期估算均价', ...mockData() },
//     { id: '中长期电费', ...mockData() },
//     { id: '日前出清电量', colSpan: data.length + 1 },
//     { id: '日前正现货电量', ...mockData() },
//     { id: '日前负现货电量', ...mockData() },
//     { id: '日前正现货电价', ...mockData() },
//     { id: '日前负现货电价', ...mockData() },
//     { id: '日前偏差电费', ...mockData() },
//     { id: '实时上网电量', colSpan: data.length + 1 },
//     { id: '实时正现货电量', ...mockData() },
//     { id: '实时负现货电量', ...mockData() },
//     { id: '实时正现货电价', ...mockData() },
//     { id: '实时负现货电价', ...mockData() },
//     { id: '实时偏差电费', ...mockData() },
//     { id: '省间现货电量', ...mockData() },
//     { id: '省间现货电价', ...mockData() },
//     { id: '省间现货电费', ...mockData() },
//     { id: '合计电费', ...mockData() },
//     { id: '当日平均电价', ...mockData() },
//     { id: '当日执行偏差考核费用', ...mockData() },
//     { id: '当日新能源盈余回收费用', ...mockData() },
//     { id: '月累计执行偏差考核费用', ...mockData() },
//     { id: '月累计新能源盈余回收费用', ...mockData() },
//     { id: '月累平均电价', ...mockData() },
//   ];
//   return {
//     columns,
//     dataSource,
//   };
// };

export const exportFn = (tables, imgs) => {
  const filename = '大唐甘肃发电有限公司市场交易日报';
  let row = 0;
  // 创建工作薄
  let workbook = new ExcelJS.Workbook();
  let worksheet = workbook.addWorksheet('Sheet1');
  imgs.map((base64Image) => {
    const imageId2 = workbook.addImage({
      base64: base64Image,
      extension: 'png',
    });
    worksheet.addImage(imageId2, `A${row + 1}:K${row + 10}`);
    row += 10;
  });
  // 数据
  tables.map(({ columns, dataSource }) => {
    const expData = [
      columns.map((x) => x.name),
      ...dataSource.map((r) => columns.map(({ code }) => r[code])),
    ];
    expData.map((x) => {
      worksheet.addRow(x, 'i');
      row += 1;
    });
  });
  // 下载
  workbook.xlsx.writeBuffer().then((buffer) => {
    saveAs(new Blob([buffer], { type: 'application/octet-stream' }), `${filename}.xlsx`);
  });
};
